package org.NooLab.glue.net.http.rest;

import java.io.IOException;
import java.util.Observer;
import java.util.Vector;

 
import org.NooLab.glue.components.DataReceiverIntf;
import org.NooLab.glue.net.DataSlot;
import org.NooLab.glue.net.UserData;
import org.NooLab.utilities.logging.PrintLog;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;


/**
 * in the context of http(-rest)-clients, receivers are virtual receivers, at least
 * on the side of the client, while the server does not play the role of the "sender"
 * 
 * The receiver comes in two modes
 * - server side : no outgoing polling, as a server, it just sits there as a http-server
 * - client side : polling to the server and issuing a get-request, if there is 
 *                 any need or indication to do so
 * 
 * 
 */
public class HttpRestletReceiver implements Runnable, DataReceiverIntf{

	
	String resourceURI ="" ;
	ClientResource glueResource;
	
	// RestletClient restletClient;
	HttpRestletSender restletClient ;
	
	HttpResponseHandlerIntf responseDigester;
	
	PollingResponseHandler primaryResponseHandler;
	
	UserData basicuser;
	
	String[] httpmet = new String[]{"get","post","put","delete","trace","option"} ;
	Vector<String> httpmethods ;
	
	 
	boolean pollingIsActive =false;
	
	PrintLog out = new PrintLog(4,true) ;
	
	Observer observer;
	
	Thread pollThrd;
	
	 
	/**
	    the interface "GlueRestletHandlerIntf" introduces the following callbacks 
	    for routing the received messages
	    
			getRestletMessageStr( String msg ) ;
			getRestletMessageObj( DataSlot dataslot ) ;
	
		
	 */
	public HttpRestletReceiver( String resourceurl,
								String role,
			  					HttpResponseHandlerIntf responsedigester,
			  					String description){
		
		resourceURI = resourceurl;
		responseDigester = responsedigester;
		
		primaryResponseHandler = new PollingResponseHandler() ;
		
		pollThrd = new Thread(this, "httprestlet-polling") ;
		pollThrd.start() ;
	
		
		basicuser = new UserData();
		basicuser.setName("noolab");
		basicuser.setPassword("glue") ;
		 
		// restletClient = new RestletClient(basicuser, out); 
		 
		restletClient = new HttpRestletSender( resourceurl,role,responsedigester, out );
		
	}


	@Override
	public void setObserver(Observer dataConnector) {
	 
		observer = dataConnector;
	}

	@Override
	public void deleteObservers() {
	 
		
	}
	

	@Override
	public int getListenPort() {
		// for http-clients, there is no such port,
		// only the message knows about it ... 
		// -> so we use that
		return 0;
	}


	@Override
	public void closePort() {
		// means, shutdown the connection...
		// in case of http-clients, this is done automatically 
	}


	
	@Override
	public void run() {
		pollingIsActive = false;
		
		try{
			int z=0;
			while ((pollingIsActive) && (z<500)){
				delay(10);
			} // isrunning ->

			
		}catch(Exception e){
			
		}
		
		
	}

 
	@SuppressWarnings("static-access")
	private void delay(int millis){
		
		try {
			Thread.currentThread().sleep(millis);
		} catch (Exception e) {}	
	}
	
} // class HttpRestletReceiver


class PollingResponseHandler {
	
	public PollingResponseHandler(){
		
	}

	 
	public void digestResponse( ) {
		String str="";
		
		
		try {
			
			 
			
		} catch (Exception e) {
			 
			e.printStackTrace();
		}
		
		
		
	}
	 
	
}







